package com.example.demo.service.impl;

import com.example.demo.mapper.AdminMapper;
import com.example.demo.pojo.*;
import com.example.demo.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin authenticate(String username, String password) {
        Admin admin = adminMapper.authenticate(username,password);
        return admin;
    }

    @Override
    public List<Department> getAllDepartment() {
        List<Department> allDepartment = adminMapper.getAllDepartment();
        return allDepartment;
    }

    @Override
    public void addDepartment(Department department) {
        adminMapper.addDepartment(department);
    }

    @Override
    public void updateDepartment(Department department) {
        adminMapper.updateDepartment(department);
    }

    @Override
    public List<Doctor> getAllDoctor() {
        List<Doctor> allDoctor = adminMapper.getAllDctor();
        return allDoctor;
    }

    @Override
    public void addDoctor(Doctor doctor) {
        adminMapper.addDoctor(doctor);
    }

    @Override
    public void updateDoctor(Doctor doctor) {
        adminMapper.updateDoctor(doctor);
    }

    @Override
    public void deleteDoctor(Long id) {
        adminMapper.deleteDoctor(id);
    }

    @Override
    public List<ScheduleInfo> getAllScheduleInfoOfDoctor() {
        List<ScheduleInfo> allScheduleInfoOfDoctor = adminMapper.getAllScheduleInfoOfDoctor();
        for (ScheduleInfo scheduleInfo : allScheduleInfoOfDoctor) {
            if(scheduleInfo.getPeriod() == 1){
                scheduleInfo.setPeriodName("上午");
            }
            else if(scheduleInfo.getPeriod() == 2){
                scheduleInfo.setPeriodName("下午");
            }
        }
        return allScheduleInfoOfDoctor;
    }

    @Override
    public void updateSchdule(ScheduleInfo scheduleInfo) {
        // 转换日期格式（避免时区问题）
        if (scheduleInfo.getScheduleDate() != null) {
            try {
                // 获取纯日期部分（YYYY-MM-DD）
                String dateStr = new SimpleDateFormat("yyyy-MM-dd")
                        .format(scheduleInfo.getScheduleDate());

                // 转换为 SQL 日期（无时间部分）
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                java.util.Date utilDate = sdf.parse(dateStr);
                java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

                // 使用纯日期格式
                scheduleInfo.setScheduleDate(sqlDate);
            } catch (ParseException e) {
                log.error("日期解析错误: {}", scheduleInfo.getScheduleDate(), e);
                scheduleInfo.setScheduleDate(null);
            }
        }
        adminMapper.updateSchdule(scheduleInfo);
    }

    @Override
    public void addSchedule(Schedule schedule) {
        schedule.setAvailableNumber(schedule.getTotalNumber());
        adminMapper.addSchedule(schedule);
    }

    @Override
    public void deleteSchedule(Long id) {
        adminMapper.deleteSchedule(id);
    }

    @Override
    public Schedule getScheduleById(Long id) {
        Schedule schedule = adminMapper.getScheduleById(id);
        return schedule;
    }

    @Override
    public List<ScheduleInfo> searchScheduleInfo(ScheduleInfo scheduleInfo) {
        // 转换日期格式（避免时区问题）
        if (scheduleInfo.getScheduleDate() != null) {
            try {
                // 获取纯日期部分（YYYY-MM-DD）
                String dateStr = new SimpleDateFormat("yyyy-MM-dd")
                        .format(scheduleInfo.getScheduleDate());

                // 转换为 SQL 日期（无时间部分）
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                java.util.Date utilDate = sdf.parse(dateStr);
                java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

                // 使用纯日期格式
                scheduleInfo.setScheduleDate(sqlDate);
            } catch (ParseException e) {
                log.error("日期解析错误: {}", scheduleInfo.getScheduleDate(), e);
                scheduleInfo.setScheduleDate(null);
            }
        }
        List<ScheduleInfo> scheduleInfos = adminMapper.searchScheduleInfo(scheduleInfo);
        for (ScheduleInfo scheduleInfo1 : scheduleInfos) {
            if(scheduleInfo1.getPeriod() == 1){
                scheduleInfo1.setPeriodName("上午");
            }
            else if(scheduleInfo1.getPeriod() == 2){
                scheduleInfo1.setPeriodName("下午");
            }
        }
        return scheduleInfos;
    }

    @Override
    public List<WaitingDetail> getALlWaitingList() {
        return adminMapper.getAllWaitingList();
    }

    @Override
    public void updateWaiting(Waiting waiting) {
        adminMapper.updateWaiting(waiting);
    }

    @Override
    public void cancelWaiting(Long id) {
        adminMapper.cancelWaiting(id);
    }

    @Override
    public void setCallTime(Long id) {
        Date callTime =  new Date();
        adminMapper.setCallTime(callTime,id);
    }

    @Override
    public void consultWaiting(Long id) {
        adminMapper.consultWaiting(id);
    }

    @Override
    public void finishWaiting(Long id) {
        adminMapper.finishWaiting(id);
    }

}
